package com.qf.controller;

import com.qf.pojo.Goods;
import com.qf.pojo.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController  {

    @Resource
    private GoodsService goodsService;

    @RequestMapping(value = "/getGoodsByType",method = RequestMethod.GET)
    public ModelAndView selectGoodsByType(HttpServletRequest req, HttpServletResponse resp,
                                          @RequestParam(value ="page",defaultValue = "1") int page,
                                          @RequestParam(value ="pr",defaultValue = "4") int pr,
                                          @RequestParam(value ="c1",defaultValue = "0")int c1,
                                          @RequestParam(value ="c2",defaultValue = "0")int c2,
                                          @RequestParam(value ="c3",defaultValue = "0")int c3,
                                          @RequestParam(value ="words",defaultValue = "")String words) {
        //分页请求参数
        PageBean pb=new PageBean();
        pb.setNowPage(page);
        pb.setRowsPerPage(pr);//默认是4

        List<Goods> goodsList=goodsService.selectSome(c1,c2,c3,words,pb);
        int allRows=goodsService.getAllRows(c1,c2,c3,words);
        req.setAttribute("c1",c1);
        req.setAttribute("c2",c2);
        req.setAttribute("c3",c3);
        req.setAttribute("words",words);
        pb.setRowsNum(allRows);
        ModelAndView mv=new ModelAndView();
        mv.addObject("goodslist", goodsList);
        mv.addObject("pb",pb);
        mv.setViewName("list");
        return mv;
    }

    @RequestMapping(value = "/getDetails",method = RequestMethod.GET)
    public String selectDetails(int gid, Model model){
        Goods goods=goodsService.getDetailById(gid);
        model.addAttribute("gooddetail",goods);
        return "/detail";
    }
}
